<?php
//-------------------------------------------------------------------------
// OVIDENTIA http://www.ovidentia.org
// Ovidentia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//-------------------------------------------------------------------------
/**
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
 * @copyright Copyright (c) 2011 by CANTICO ({@link http://www.cantico.fr})
 */



/**
 * Echo a json array of groupss.
 *
 * @return void
 */
function widgets_getJsonGroups()
{
	if (!isset($_SESSION['Widget_Acl_uid']) || $_SESSION['Widget_Acl_uid'] !== bab_rp('uid'))
	{
		return 'access denied';
	}
	
	
	$id_parent = bab_rp('id_parent', null);
	
	if (null === $id_parent)
	{
		die('missing id_parent');
	}
	
	// get groups from API
	
	$groups = bab_getGroups($id_parent, false);
	$json = array();
	foreach ($groups['id'] as $key => $id) {

		$hasChildNodes = 'false';
		if (count(bab_getGroups($id)) > 0)
		{
			$hasChildNodes = 'true';
		}
		
		$json[] = "{ 'id' : '$id' ,'name' : '".bab_toHtml($groups['name'][$key], BAB_HTML_JS)."', 'hasChildNodes' : $hasChildNodes, 'setOfGroups' : false }";
	}
	
	

	// add group set if root
	
	if (0 === (int) $id_parent)
	{
		$groups = bab_getGroups(null, false);
		
		if (isset($groups['id']) && is_array($groups['id']))
		{
			foreach ($groups['id'] as $key => $id) {
				$json[] = "{ 'id' : '$id' ,'name' : '".bab_toHtml($groups['name'][$key], BAB_HTML_JS)."', 'hasChildNodes' : false, 'setOfGroups' : true }";
			}
		}
	}

	echo '['.implode(',', $json).']';
}



function widgets_getJsonGroupInfos()
{
	if (!isset($_SESSION['Widget_Acl_uid']) || $_SESSION['Widget_Acl_uid'] !== bab_rp('uid'))
	{
		return 'access denied';
	}
	
	
	$groups = bab_rp('groups', null);
	
	if (null === $groups)
	{
		die('missing groups');
	}
	
	
	$arr = explode(',', $groups);
	$json = array();
	
	foreach($arr as $g)
	{
		if (empty($g))
		{
			continue;
		}
		
		if (mb_strlen($g) > 1 && '+' === mb_substr($g,-1))
		{
			$id_group = (int) mb_substr($g,0,-1);
			$withChildNodes = 'true';
		} else {
			
			$id_group = (int) $g;
			$withChildNodes = 'false';
		}
		
		if (bab_Groups::isGroupSet($id_group))
		{
			$setOfGroups = 'true';
		} else {
			$setOfGroups = 'false';
		}
		
		$json[] = "{ 'id' : '$id_group' ,'name' : '".bab_toHtml(bab_getGroupName($id_group, true), BAB_HTML_JS)."', 'withChildNodes' : $withChildNodes, 'setOfGroups' : $setOfGroups }";
	}
	
	
	
	
	
	echo '['.implode(',', $json).']';
}



switch(bab_rp('idx'))
{
	case 'get':
		widgets_getJsonGroups();
		die;
		
	case 'infos':
		widgets_getJsonGroupInfos();
		die;
}